package com.wans.guli.integration.oss;


import com.qiniu.util.Auth;
import com.wans.guli.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Date;
import java.util.HashMap;

@RestController
@RequestMapping("/qiniu/oss")
@Slf4j
public class QiniuController {
    // 七牛云配置（替换为你的信息）
    @Value("${qiniu.accessKey}")
    private String ACCESS_KEY;
    @Value("${qiniu.secretKey}")
    private String SECRET_KEY;
    @Value("${qiniu.bucketName}")
    private String BUCKET_NAME;
    @Value("${qiniu.host}")
    private String HOST;

    @GetMapping("/policy")
    public R getUploadToken() {
        // 生成上传凭证


        Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
        String upToken = auth.uploadToken(BUCKET_NAME, null, 5, null);
        log.warn("upToken: {}", upToken);

        // yyyy-MM-dd
        String dir = new SimpleDateFormat("yyyy-MM-dd").format(Date.from(Instant.now()));
        log.warn("dir: {}", dir);

        HashMap<String, String> result = new HashMap<>();
        result.put("policy", upToken);
        result.put("dir", dir);
        result.put("host", HOST);
        return R.success(result);
    }
}